import gtk
import threading
STATUS_UP=1
STATUS_DOWN=-1
STATUS_EQUAL=0

class Tips:

	def __init__(self,main_window):
		self.tray=gtk.status_icon_new_from_file('go-next.png')
		self.tray.connect("activate",self.on_activate)
		self.visible=True
		#self.update_status(STATUS_EQUAL)
		self.mw=main_window
		self.tray.set_visible(True)
		self.tip_lock=threading.Lock()
		
	def on_activate(self,tray):
		self.visible=not self.visible
		if(self.visible):self.mw.show()
		else:self.mw.hide()
		
	def update_status(self,status):
		if(status==STATUS_DOWN):self.tray.set_from_file('go-down.png')
		elif(status==STATUS_UP):self.tray.set_from_file('go-up.png')
		else:self.tray.set_from_file('go-next.png')
		
	def set_tips(self,text):
		self.tray.set_tooltip(text)
		
	def set_blinking(self,flag):
		self.tray.set_blinking(flag)
		
	def show_stocks_on_tips(self,stocks):
		if(len(stocks)<1):return
		text="favorits %s\n-----------\n" % stocks[0].time;
		for x in stocks:
			text=text+"%s %+0.2f%% | %0.2f | %0.2f | %0.2f\n" % (x.name,x.change_percent,x.change,x.price_now,x.price_lastday)
		gtk.gdk.threads_enter()
		try:
			self.set_tips(text)
		finally:
			gtk.gdk.threads_leave()
		